<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div v-if="crud.props.searchToggle">
        <!-- 搜索 -->
        <el-input
          v-model="queryForm.name"
          clearable
          size="small"
          placeholder="规则名称"
          style="width:200px"
          class="filter-item"
        />
        <el-select
          v-model="queryForm.way"
          clearable
          size="small"
          placeholder="脱敏方式"
          class="filter-item"
        >
          <el-option
            v-for="item in wayList"
            :key="item"
            :label="item"
            :value="item"
          />
        </el-select>
        <rrOperation :crud="crud" />
      </div>
      <crudOperation
        show=""
        :permission="permission"
      />
    </div>
    <!--表单渲染-->
    <el-dialog
      append-to-body
      :close-on-click-modal="false"
      :before-close="crud.cancelCU"
      :visible.sync="crud.status.cu > 0"
      title="新增"
      width="1000px"
    >
      <div v-if="activeName == 1">
        <el-form
          :inline="true"
          label-width="120px"
        >
          <div style="font-size: 16px;font-weight:bold;margin-bottom:2%">规则配置</div>
          <el-form-item label="脱敏规则编号">
            <el-input
              v-model="addForm.code"
              placeholder="请输入"
            />
          </el-form-item>
          <el-form-item label="脱敏规则名称">
            <el-input
              v-model="addForm.name"
              placeholder="请输入"
            />
          </el-form-item>
          <el-form-item label="脱敏方式">
            <el-select
              v-model="addForm.way"
              size="small"
              placeholder="请输入"
              class="filter-item"
              style="width: 190px;"
            >
              <el-option
                v-for="item in wayList"
                :key="item"
                :label="item"
                :value="item"
              />
            </el-select>
            <el-tooltip
              v-if="addForm.way == '固定映射'"
              placement="top"
            >
              <template #content> 算法说明：<br>
                通过设置映射种子，在映射种子不变的情况下，相同原数据脱敏后结果相同，并保留原始业务特征
                <br>
                示例：<br>
                映射设置：李四、原数据：张三、脱敏结果：李四
                <br>
                参考场景：<br>
                中文姓名、身份证、电话、银行卡、电子邮箱、地址、IP地址<br>
                日期、通用字符串、键、货币金额<br>
                机构登记号、资格证书、执业证书<br>
                营业执照、社会统一信用代码、组织机构代码、税务登记证、开户许可证<br>
                护照、军官证、中国护照、港澳通行证、永久居住证、台湾同胞大陆通行证<br>
                证券名称、证券代码、基金名称、基金代码</template>
              <i
                class="el-icon-question"
                style="font-size: 18px;"
              />
            </el-tooltip>
            <el-tooltip
              v-if="addForm.way == '随即映射'"
              placement="top"
            >
              <template #content> 算法说明：<br>
                对数值、字符或字符串进行随机，并保留原业务特征
                <br>
                示例：<br>
                原数据：19841222、脱敏结果：19900211
                <br>
                参考场景：<br>
                中文姓名、身份证、电话、银行卡、电子邮箱、地址、邮政编码、IP地址、CCV码<br>
                货币金额、通用字符串、字符串<br>
                机构登记号、资格证书、执业证书<br>
                营业执照、社会统一信用代码、税务登记证、开户许可证、组织机构代码、组织机构名称<br>
                护照、军官证、中国护照、港澳通行证、永久居住证、台湾同胞大陆通行证<br>
                证券名称、证券代码、基金名称、基金代码</template>
              <i
                class="el-icon-question"
                style="font-size: 18px;"
              />
            </el-tooltip>
            <el-tooltip
              v-if="addForm.way == '遮盖脱敏'"
              placement="top"
            >
              <template #content> 算法说明：<br>
                通过设置遮盖符，对原数据全部或部分进行遮盖处理
                <br>
                示例：<br>
                遮盖符：*、原数据：13512345678、脱敏结果：135****5678
                <br>
                参考场景：<br>
                中文姓名、身份证、电话、银行卡、电子邮箱、地址、CCV码、IP地址<br>
                组织机构代码、组织机构名称、营业执照、社会统一信用代码<br>
                机构登记号、资格证书、执业证书<br>
                护照、税务登记证、开户许可证、军官证、中国护照、港澳通行证、永久居住证、台湾同胞大陆通行证<br>
                证券名称、证券代码、基金名称、基金代码</template>
              <i
                class="el-icon-question"
                style="font-size: 18px;"
              />
            </el-tooltip>
            <el-tooltip
              v-if="addForm.way == '范围随机'"
              placement="top"
            >
              <template #content> 算法说明：<br>
                对日期或金额，在一个指定的范围内进行随机，并保留原业务特征
                <br>
                示例：<br>
                范围1000至9999<br>
                原数据：38472.00<br>
                脱敏结果：8394.00
                <br>
                参考场景：<br>
                日期<br>
                货币金额<br>
                通用数值</template>
              <i
                class="el-icon-question"
                style="font-size: 18px;"
              />
            </el-tooltip>
            <el-tooltip
              v-if="addForm.way == '取整'"
              placement="top"
            >
              <template #content> 算法说明：<br>
                对数字位数进行取整
                <br>
                示例：<br>
                如取整位数为2，则1988.65脱敏后为1900<br>
                如12345678，整数“3位”，则为12345000
                <br>
                参考场景：<br>
                货币金额<br>
                通用数值</template>
              <i
                class="el-icon-question"
                style="font-size: 18px;"
              />
            </el-tooltip>
          </el-form-item>
          <el-divider />
          <div style="font-size: 16px;font-weight:bold;margin-bottom:2%">规则详细配置</div>
          <el-form-item
            v-if="addForm.way == '固定映射'"
            label="脱敏数据类型"
          >
            <el-select
              v-model="addForm.type"
              clearable
              size="small"
              placeholder="请输入"
              class="filter-item"
              style="width: 190px;"
            >
              <el-option
                v-for="item in typeList"
                :key="item"
                :label="item"
                :value="item"
              />
            </el-select>
          </el-form-item>
          <el-form-item
            v-if="addForm.way == '固定映射'"
            label="映射后数据内容"
          >
            <el-input
              v-model="addForm.data"
              placeholder="请输入映射后数据内容，如姓名映射李四"
            />
          </el-form-item>
          <el-form-item
            v-if="addForm.way == '随即映射'"
            label="脱敏数据类型"
          >
            <el-select
              v-model="addForm.type"
              clearable
              size="small"
              placeholder="请输入"
              class="filter-item"
              style="width: 190px;"
            >
              <el-option
                v-for="item in typeList"
                :key="item"
                :label="item"
                :value="item"
              />
            </el-select>
          </el-form-item>
          <el-form-item
            v-if="addForm.way == '遮盖脱敏'"
            label="脱敏数据类型"
          >
            <el-select
              v-model="addForm.type"
              clearable
              size="small"
              placeholder="请输入"
              class="filter-item"
              style="width: 190px;"
            >
              <el-option
                v-for="item in typeList1"
                :key="item"
                :label="item"
                :value="item"
              />
            </el-select>
          </el-form-item>
          <el-form-item
            v-if="addForm.way == '遮盖脱敏'"
            label="遮盖区间"
          >
            <el-input
              v-model="addForm.first"
              style="width:80px"
            /> 至 <el-input
              v-model="addForm.last"
              style="width:80px"
            />
          </el-form-item>
          <el-form-item
            v-if="addForm.way == '遮盖脱敏'"
            label="遮盖符"
          >
            <el-input
              v-model="addForm.data"
              placeholder="请输入一位遮盖符号，通常为*"
            />
          </el-form-item>
          <el-form-item
            v-if="addForm.way == '范围随机'"
            label="脱敏数据类型"
          >
            <el-select
              v-model="addForm.type"
              clearable
              size="small"
              placeholder="请输入"
              class="filter-item"
              style="width: 190px;"
            >
              <el-option
                v-for="item in typeList2"
                :key="item"
                :label="item"
                :value="item"
              />
            </el-select>
          </el-form-item>
          <el-form-item
            v-if="addForm.way == '范围随机'"
            label="随机区间"
          >
            <el-input
              v-model="addForm.first"
              style="width:80px"
            /> 至 <el-input
              v-model="addForm.last"
              style="width:80px"
            />
          </el-form-item>
          <el-form-item
            v-if="addForm.way == '取整'"
            label="脱敏数据类型"
          >
            <el-select
              v-model="addForm.type"
              clearable
              size="small"
              placeholder="请输入"
              class="filter-item"
              style="width: 190px;"
            >
              <el-option
                v-for="item in typeList3"
                :key="item"
                :label="item"
                :value="item"
              />
            </el-select>
          </el-form-item>
          <el-form-item
            v-if="addForm.way == '取整'"
            label="取整位数设置"
          >
            <el-input
              v-model="addForm.data"
              placeholder="请输入取整位数"
            />
          </el-form-item>
          <el-divider />
          <div style="font-size: 16px;font-weight:bold;margin-bottom:2%">数据脱敏模拟预览</div>
          <el-form-item label="预脱敏数据内容">
            <el-input
              v-model="addForm.before"
              placeholder="请输入"
            />
          </el-form-item>
          <el-form-item label="脱敏后数据内容">
            <el-input
              v-model="addForm.after"
              placeholder="请输入"
            />
          </el-form-item>
          <el-form-item label="备注信息">
            <el-input
              v-model="addForm.remark"
              placeholder="请输入"
              type="textarea"
              style="width:700px"
            />
          </el-form-item>
        </el-form>
        <div style="text-align: center;">
          <el-button
            type="primary"
            @click="next"
          >下一步</el-button>
        </div>
      </div>
      <div v-if="activeName == 2">
        <el-button
          type="primary"
          size="mini"
          @click="add"
        >新增</el-button>
        <el-button
          type="primary"
          size="mini"
          @click="del"
        >删除</el-button>
        <el-table
          :data="infoList"
          @selection-change="handleSelectionChange"
        >
          <el-table-column
            type="selection"
            width="55"
          />
          <el-table-column
            label="库名"
            prop="lib"
          >
            <template slot-scope="scope">
              <el-select
                v-model="scope.row.lib"
                clearable
                size="small"
                placeholder="请输入"
                class="filter-item"
              >
                <el-option
                  v-for="item in libList"
                  :key="item"
                  :label="item"
                  :value="item"
                />
              </el-select>
            </template>
          </el-table-column>
          <el-table-column
            label="表名"
            prop="tbl"
          >
            <template slot-scope="scope">
              <el-select
                v-model="scope.row.tbl"
                clearable
                size="small"
                placeholder="请输入"
                class="filter-item"
              >
                <el-option
                  v-for="item in tblList"
                  :key="item"
                  :label="item"
                  :value="item"
                />
              </el-select>
            </template>
          </el-table-column>
          <el-table-column
            label="字段名"
            prop="field"
          >
            <template slot-scope="scope">
              <el-select
                v-model="scope.row.field"
                clearable
                size="small"
                placeholder="请输入"
                class="filter-item"
              >
                <el-option
                  v-for="item in fieldList"
                  :key="item"
                  :label="item"
                  :value="item"
                />
              </el-select>
            </template>
          </el-table-column>
          <el-table-column
            label="字段中文名"
            prop="chinese"
          />
          <el-table-column
            label="脱敏前数据"
            prop="before"
          />
          <el-table-column
            label="脱敏后预览"
            prop="after"
          />
        </el-table>
        <div style="text-align: center;margin-top:3%">
          <el-button
            type="primary"
            @click="step"
          >上一步</el-button>
          <el-button
            type="primary"
            @click="save"
          >保存</el-button>
        </div>
      </div>
    </el-dialog>
    <!--表格渲染-->
    <el-table
      ref="table"
      :data="tableList"
      row-key="id"
    >
      <el-table-column
        type="selection"
        width="55"
      />
      <el-table-column
        label="规则编码"
        prop="code"
      />
      <el-table-column
        label="规则名称"
        prop="name"
      />
      <el-table-column
        label="脱敏方式"
        prop="way"
      />
      <el-table-column
        label="创建时间"
        prop="time"
      />
      <el-table-column
        label="操作"
        align="center"
        width="300"
      >
        <template>
          <el-button type="text">详情</el-button>
          <el-button type="text">编辑</el-button>
          <el-button type="text">删除</el-button>
          <el-button type="text">清单维护</el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
import CRUD, { presenter, header, form, crud } from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
// import udOperation from '@crud/UD.operation'

// crud交由presenter持有
const defaultCrud = CRUD({ title: '', url: '', crudMethod: {} })
const defaultForm = {}
export default {
  name: '',
  components: { crudOperation, rrOperation },
  mixins: [presenter(defaultCrud), header(), form(defaultForm), crud()],
  data() {
    return {
      permission: {
        add: [],
        edit: ['edit'],
        del: ['del']
      },
      queryForm: {
        name: '',
        way: ''
      },
      tableList: [
        {
          code: 'R0001',
          name: '手机号脱敏',
          way: '固定映射',
          time: '2023-08-02 14:25"11'
        },
        {
          code: 'R0002',
          name: '身份证脱敏',
          way: '随即映射',
          time: '2023-08-02 14:25"11'
        },
        {
          code: 'R0003',
          name: '卡号脱敏',
          way: '遮盖脱敏',
          time: '2023-08-02 14:25"11'
        },
        {
          code: 'R0004',
          name: '金额脱敏',
          way: '范围随机',
          time: '2023-08-02 14:25"11'
        }
      ],
      wayList: ['固定映射', '随即映射', '遮盖脱敏', '范围随机', '取整'],
      typeList: ['字符型', '数值型', '日期型'],
      typeList1: ['字符型', '日期型'],
      typeList2: ['数值型', '日期型'],
      typeList3: ['数值型'],
      addForm: {
        name: '',
        code: '',
        way: '固定映射',
        type: '',
        data: '',
        before: '',
        after: '',
        remark: '',
        first: '',
        last: ''
      },
      activeName: 1,
      infoList: [
        {
          lib: 'lib_a',
          tbl: 'tbl_a',
          field: 'field_a',
          chinese: '手机号',
          before: '18234512463',
          after: '182*****2463',
          num: 1
        }
      ],
      multipleSelection: [],
      libList: ['lib_a', 'lib_b', 'lib_c'],
      tblList: ['tbl_a', 'tbl_b', 'tbl_c'],
      fieldList: ['field_a', 'field_b', 'field_c'],
      num: 1
    }
  },
  methods: {
    next() {
      this.activeName++
    },
    step() {
      this.activeName--
    },
    save() {
      this.activeName = 1
      this.crud.cancelCU()
    },
    handleSelectionChange(val) {
      this.multipleSelection = val
    },
    add() {
      this.num += 1
      const obj = { lib: '', tbl: '', field: '', chinese: '', before: '', after: '', mun: this.num }
      this.infoList.push(obj)
    },
    del() {
      this.infoList.forEach((item, index) => {
        this.multipleSelection.forEach((val) => {
          if (item.num === val.num) {
            this.infoList.splice(index, 1)
          }
        })
      })
      this.$refs.table.clearSelection()
    }
  }
}
</script>
